package client;

import com.sjn.*;
import javax.swing.event.*;
import java.io.*;

public class CodePanel extends javax.swing.JPanel {

    public CodePanel(String title, int i, javax.swing.JTabbedPane jtp) {
        language = "Cpp";
        initComponents();
        name = title;
        panelint = i;
        tabbedPane = jtp;
        JSP_Code.setViewportView(JEP_Code);
        JEP_Code = new ClipTextArea(JSP_Code);
        JTP_Info.addTab("Test", new TestPanel());
    }

    public void setCode(String str) {
        JEP_Code.setText(str);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar = new javax.swing.JToolBar();
        JCB_Language = new javax.swing.JComboBox();
        JB_Save = new javax.swing.JButton();
        JB_Compile = new javax.swing.JButton();
        JB_Test = new javax.swing.JButton();
        JB_Submit = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        JSP_Code = new javax.swing.JScrollPane();
        JEP_Tmep = new javax.swing.JEditorPane();
        JTP_Info = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        JTA_CompileInfo = new javax.swing.JTextArea();

        setLayout(new java.awt.BorderLayout());

        jToolBar.setFloatable(false);

        JCB_Language.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cpp", "C", "Java", "Pascal" }));
        JCB_Language.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JCB_LanguageActionPerformed(evt);
            }
        });
        jToolBar.add(JCB_Language);

        JB_Save.setText("Save");
        JB_Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SaveActionPerformed(evt);
            }
        });
        jToolBar.add(JB_Save);

        JB_Compile.setText("Compile");
        JB_Compile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_CompileActionPerformed(evt);
            }
        });
        jToolBar.add(JB_Compile);

        JB_Test.setText("Test");
        JB_Test.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_TestActionPerformed(evt);
            }
        });
        jToolBar.add(JB_Test);

        JB_Submit.setText("Submit");
        JB_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SubmitActionPerformed(evt);
            }
        });
        jToolBar.add(JB_Submit);

        add(jToolBar, java.awt.BorderLayout.NORTH);

        jSplitPane1.setDividerLocation(300);
        jSplitPane1.setDividerSize(7);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setOneTouchExpandable(true);

        JSP_Code.setViewportView(JEP_Tmep);

        jSplitPane1.setTopComponent(JSP_Code);

        jPanel1.setLayout(new java.awt.BorderLayout());

        JTA_CompileInfo.setColumns(20);
        JTA_CompileInfo.setRows(5);
        jScrollPane1.setViewportView(JTA_CompileInfo);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        JTP_Info.addTab("CompileInformation", jPanel1);

        jSplitPane1.setRightComponent(JTP_Info);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void JB_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SaveActionPerformed
    javax.swing.JFileChooser chooser = new javax.swing.JFileChooser("./test/src/");
    chooser.setSelectedFile(new File(name + "." + language.toLowerCase()));
    if (chooser.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
        try {
            File file = chooser.getSelectedFile();
            name = file.getName();
            if (name.indexOf(".") != -1) {
                name = name.substring(0, name.indexOf("."));
            }
            tabbedPane.setTitleAt(panelint, name);
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(JEP_Code.getText());
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}//GEN-LAST:event_JB_SaveActionPerformed

    private void JCB_LanguageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JCB_LanguageActionPerformed
        language = JCB_Language.getSelectedItem().toString();
        JEP_Code.setLanguage(language);
}//GEN-LAST:event_JCB_LanguageActionPerformed

private void JB_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SubmitActionPerformed
    Control.setSingleAnswerDoc(name, language, JEP_Code.getText());
    javax.swing.JDialog dialog = new javax.swing.JDialog();
    SubmitPanel submitpanel = new SubmitPanel(dialog);
    Control.getMainFrame().newDialog(dialog, submitpanel, "Submit");
}//GEN-LAST:event_JB_SubmitActionPerformed

    private void JB_CompileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_CompileActionPerformed
        try {
            JTA_CompileInfo.setText("Compiling~~~");
            Control.setSingleAnswerDoc(name, language, JEP_Code.getText());
            Control.Compile();
            JTA_CompileInfo.setText(Control.getCompileOut());
            System.out.println(Control.getCompileOut());
        } catch (Exception e) {
            e.printStackTrace();
            JTA_CompileInfo.setText("Set your Compile Path~");
        }
    }//GEN-LAST:event_JB_CompileActionPerformed

    private void JB_TestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_TestActionPerformed
        JTP_Info.setSelectedIndex(1);
    }//GEN-LAST:event_JB_TestActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_Compile;
    private javax.swing.JButton JB_Save;
    private javax.swing.JButton JB_Submit;
    private javax.swing.JButton JB_Test;
    private javax.swing.JComboBox JCB_Language;
    private javax.swing.JEditorPane JEP_Tmep;
    private javax.swing.JScrollPane JSP_Code;
    private javax.swing.JTextArea JTA_CompileInfo;
    private javax.swing.JTabbedPane JTP_Info;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JToolBar jToolBar;
    // End of variables declaration//GEN-END:variables
    private String language = "Cpp";
    private String name;
    private int panelint;
    private javax.swing.JTabbedPane tabbedPane;
    private ClipTextArea JEP_Code;
}
